package com.ruicar.afs.cloud.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.product.condition.ExtrasProgramCondition;
import com.ruicar.afs.cloud.product.condition.ExtrasProgramConfigCondition;
import com.ruicar.afs.cloud.product.entity.ExtrasProjectProgramInfo;
import com.ruicar.afs.cloud.product.enums.MoveTypeEnum;
import com.ruicar.afs.cloud.product.mapper.ExtrasProgramConfigMapper;
import com.ruicar.afs.cloud.product.service.ExtrasProgramConfigService;
import com.ruicar.afs.cloud.product.vo.ExtrasInfoVo;
import com.ruicar.afs.cloud.product.vo.ExtrasProgramInfoVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xudongdong
 * @date 2020-05-12
 */
@Service
public class ExtrasProgramConfigServiceImpl extends ServiceImpl<ExtrasProgramConfigMapper, ExtrasProjectProgramInfo> implements ExtrasProgramConfigService {
    @Override
    public IPage<List<ExtrasProgramInfoVo>> getExtrasConfigProgramList(Page page, ExtrasProgramConfigCondition extrasConfigProgram) {
        return baseMapper.getExtrasConfigProgramList(page, extrasConfigProgram);
    }

    @Override
    public List<ExtrasProgramInfoVo> getExtrasConfigProgramList(ExtrasProgramConfigCondition extrasConfigProgram) {
        return baseMapper.getExtrasConfigProgramList(extrasConfigProgram);
    }

    @Override
    public IPage<List<ExtrasInfoVo>> getselectProductData(Page page,ExtrasProgramCondition extrasProgramCondition) {
        return baseMapper.getselectProductData(page,extrasProgramCondition);
    }

    //原子移动
    public void moveTemplateAll(ExtrasProgramConfigCondition productCondition) {

        String extrasPlanId = productCondition.getExtrasPlanId();
        //得到限值
        List<ExtrasProjectProgramInfo> listAtom = this.list(Wrappers.<ExtrasProjectProgramInfo>query().lambda()
                .eq(ExtrasProjectProgramInfo::getExtrasPlanId, extrasPlanId)
                .orderByAsc(ExtrasProjectProgramInfo::getSerialNumber)
        );
        int limitValue = listAtom.size();
        //修改的值
        List<ExtrasProjectProgramInfo> listResult = new ArrayList<>();
        //修改排序值
        Long[] ids = productCondition.getIds();
        for (int i = 0; i < ids.length; i++) {
            ExtrasProjectProgramInfo extrasProjectProgramInfo = this.getById(ids[i]);

            int replaceNum = extrasProjectProgramInfo.getSerialNumber();

            if (AfsEnumUtil.key(MoveTypeEnum.MOVE_UP).equals(productCondition.getMoveType())) {
                if (replaceNum > 0) {//第一条不上移
                    listAtom.add(replaceNum - 1, listAtom.get(replaceNum));
                    listAtom.add(replaceNum + 1, listAtom.get(replaceNum));
                    listAtom.remove(replaceNum);
                    listAtom.remove(replaceNum + 1);
                }
            } else if (AfsEnumUtil.key(MoveTypeEnum.MOVE_DOWN).equals(productCondition.getMoveType())) {
                if (replaceNum < limitValue - 1) {//最后一条不下移
                    listAtom.add(replaceNum, listAtom.get(replaceNum + 1));
                    listAtom.add(replaceNum + 2, listAtom.get(replaceNum + 1));
                    listAtom.remove(replaceNum + 1);
                    listAtom.remove(replaceNum + 2);
                }
            } else if (AfsEnumUtil.key(MoveTypeEnum.MOVE_TOP).equals(productCondition.getMoveType())) {
                extrasProjectProgramInfo.setSerialNumber(extrasProjectProgramInfo.getSerialNumber() - limitValue);
            } else if (AfsEnumUtil.key(MoveTypeEnum.MOVEA_END).equals(productCondition.getMoveType())) {
                extrasProjectProgramInfo.setSerialNumber(extrasProjectProgramInfo.getSerialNumber() + limitValue);
            }
            listResult.add(extrasProjectProgramInfo);
        }
        if (AfsEnumUtil.key(MoveTypeEnum.MOVE_UP).equals(productCondition.getMoveType()) || AfsEnumUtil.key(MoveTypeEnum.MOVE_DOWN).equals(productCondition.getMoveType())) {
            this.sequenceOrder(listAtom, listResult);
        }
        if (AfsEnumUtil.key(MoveTypeEnum.MOVE_TOP).equals(productCondition.getMoveType()) || AfsEnumUtil.key(MoveTypeEnum.MOVEA_END).equals(productCondition.getMoveType())) {
            //修改移动的排序值
            this.updateBatchById(listResult);
            //原子进行重新排序
            this.sequenceOrderById(extrasPlanId);
        }
    }

    //原子进行重新排序
    public void sequenceOrderById(String id) {
        List<ExtrasProjectProgramInfo> listResult = new ArrayList<>();
        //得到限值
        List<ExtrasProjectProgramInfo> listAtom = this.list(Wrappers.<ExtrasProjectProgramInfo>query().lambda()
                .eq(ExtrasProjectProgramInfo::getExtrasPlanId, id)
                .orderByAsc(ExtrasProjectProgramInfo::getSerialNumber)
        );
        this.sequenceOrder(listAtom, listResult);
    }

    //复制方案
    @Override
    public void copyPlan(Long oldPlanID, String newPlanID) {
        List<ExtrasProjectProgramInfo> list = this.list(Wrappers.<ExtrasProjectProgramInfo>query().lambda()
                .eq(ExtrasProjectProgramInfo::getExtrasPlanId,String.valueOf(oldPlanID))
                .orderByAsc(ExtrasProjectProgramInfo::getSerialNumber)
        );
        for (ExtrasProjectProgramInfo extrasProjectProgramInfo : list) {
            this.copyOne(extrasProjectProgramInfo, newPlanID);
        }
    }

    //复制单个
    public void copyOne(ExtrasProjectProgramInfo extrasProjectProgramInfo, String newPlanID) {
        extrasProjectProgramInfo.setId(null);
        extrasProjectProgramInfo.setCreateBy(null);
        extrasProjectProgramInfo.setCreateTime(null);
        extrasProjectProgramInfo.setExtrasPlanId(newPlanID);
        this.save(extrasProjectProgramInfo);
    }

    //排序
    public void sequenceOrder(List<ExtrasProjectProgramInfo> listAtom, List<ExtrasProjectProgramInfo> listResult) {
        if (listAtom != null && listAtom.size() > 0) {
            for (int i = 0; i < listAtom.size(); i++) {
                ExtrasProjectProgramInfo productTemplateAtom = listAtom.get(i);
                productTemplateAtom.setSerialNumber(i);
                listResult.add(productTemplateAtom);
            }
            this.updateBatchById(listResult);
        }
    }

}
